﻿using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System.Text;

namespace TopicSubsciber
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var factory = new ConnectionFactory() { HostName = "localhost" };
            var connection = factory.CreateConnection();
            var channel = connection.CreateModel();
            channel.ExchangeDeclare(exchange: "topic_log", type: "topic");
            var queueName = channel.QueueDeclare().QueueName;
            if (args.Length < 1)
            {
                return;
            }
            foreach (var bindingKey in args)
            {
                channel.QueueBind(queue: queueName, exchange: "topic_log", routingKey: bindingKey);
            }
            Console.WriteLine("[*] Waiting for message.");
            var consumer = new EventingBasicConsumer(channel);
            consumer.Received += (sender, ea) =>
            {
                var body = ea.Body.ToArray();
                var message=Encoding.UTF8.GetString(body);
                var routingKey = ea.RoutingKey;
                Console.WriteLine($"[x] Received '{routingKey}':'{message}'");
            };
            channel.BasicConsume(queue: queueName, autoAck: true, consumer: consumer);
            Console.ReadLine();
        }
    }
}